package com.secsys.action.finance;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.secsys.action.BaseAction;
import com.secsys.dto.AssistDto;
import com.secsys.dto.StoFinanceprofitDTO;
import com.secsys.dto.StoStockDTO;
import com.secsys.model.TbStoFinanceprofit;
import com.secsys.service.StoFinanceProfitService;
import com.secsys.service.StoStockService;
import com.secsys.util.DateFormatUtils;
/**
 * 对应表TbStoFinanceprofit“上市公司资产情况(利润构成与盈利能力)”相关数据的action
 * <pre>
 * 1.处理功能（财务分析里面的）:
 * (1).净利润：prepareNetGain、searchNetGain、netGainDetail、doNetGainSearch分别为默认处理、用户搜索、详细信息、内部协助函数。
 * (2).主营业务利润：prepareMainProfit、searchMainProfit、mainProfitDetail、doMainProfitSearch分别为默认处理、用户搜索、详细信息、内部协助函数。
 *@作者 tiger.hu.tiger@gmail.com
 *@创建日期 2011-5-7
 *@版本1.0
 */
public class FinanceProfitAction extends BaseAction<TbStoFinanceprofit> {

	private TbStoFinanceprofit tbStoFinanceprofit;
	private List<StoStockDTO> stockDTOList;
	private AssistDto assistDto;
	private StoStockDTO stockDTO;
	private StoStockService stoStockService;
	
	private StoFinanceProfitService stoFinanceProfitService;
	private List<StoFinanceprofitDTO> stoFinanceprofitDTOList;
	private List<StoFinanceprofitDTO> oldYearStoFinanceprofitDTOList;
	/**
	 * 规定分页大小为10
	 */
	private final int limit=10; 
	
	

	/**
	 * 处理”主营业务利润“，默认情况处理
	 * @return
	 * @throws Exception
	 */
	public String prepareMainProfit() throws Exception{
		assistDto.setStartDate(DateFormatUtils.convertStringToDate("2008-12-31"));
		assistDto.setEndDate(DateFormatUtils.convertStringToDate("2009-12-31"));
		assistDto.setRate(15);
		assistDto.setUpdown(1);
		assistDto.setCurrentPages(0);
		assistDto.setTotalPages(0);
		
		this.doNetGainSearch();
		
		return "search";
	}
	
	/**
	 * 处理”主营业务利润“，用户搜索
	 * @return
	 * @throws Exception
	 */
	public String searchMainProfit() throws Exception{
		if (!this.doNetGainSearch()) {
			System.out.println("prepare====================");
			return "prepare";
		}
		return "search";
	}
	
	/**
	 * 处理”主营业务利润“，详细信息显示
	 * @return
	 * @throws Exception
	 */
	public String mainProfitDetail() throws Exception{
		this.stoFinanceprofitDTOList=stoFinanceProfitService.queryStoFinanceprofitByStockCode(stockDTO.getStockCode());
		stockDTO=stoStockService.findByStockCode(stockDTO.getStockCode());
		
		return "detail";
	}
	
	private boolean doMainProfitSearch(){
		Date startDate=assistDto.getStartDate();
		Date endDate=assistDto.getEndDate();
		double rate=assistDto.getRate();
		int updown=assistDto.getUpdown();
		System.out.println("开始时间="+DateFormatUtils.convertDateTimeToString(startDate)+";结束时间="
				+DateFormatUtils.convertDateTimeToString(endDate)+";百分比="+rate+";上升还是下降="+updown+"当前页"+assistDto.getCurrentPages()+"总共assistDto.getCurrentPages()"+assistDto.getCurrentPages());
		/**
		 * 设置当前页
		 */
		if (0 == assistDto.getCurrentPages()) {
			assistDto.setCurrentPages(1);
		}
		/**
		 * 偏移值= (当前第几页-1)*每页个数
		 */
		int offset = (assistDto.getCurrentPages() - 1) * limit;
		/**
		 * 查询得到的总记录数
		 */
		int num=stoFinanceProfitService.countSearchWithNetGain(startDate, endDate, rate,updown);
		System.err.println("--查询得到的总记录数-----"+num+"-----");
		/**
		 * 总共多少页
		 */
		int totalPages = (num / limit);
		if (num % limit > 0) {
			totalPages++;
		}
		
		if (assistDto.getCurrentPages() >totalPages) {
			assistDto.setCurrentPages(1); 
			return false;
		}
		System.out.println(totalPages+"总页数");
		assistDto.setTotalPages(totalPages);
		
		HashMap<String,List> map=stoFinanceProfitService.querySearchWithNetGain(startDate, endDate, rate,updown,offset,limit); 
		this.stoFinanceprofitDTOList=map.get("stoFinanceprofitDTOList");
		this.stockDTOList=map.get("stockDTOList");
		this.oldYearStoFinanceprofitDTOList=map.get("oldYearStoFinanceprofitDTOList");
		return true;
	}
	
	
	
	
	/**
	 * 处理”净资产收益率（每股收益/每股净资产）“，默认情况处理
	 * @return
	 * @throws Exception
	 */
	public String prepareNetGain() throws Exception{
		assistDto.setStartDate(DateFormatUtils.convertStringToDate("2008-12-31"));
		assistDto.setEndDate(DateFormatUtils.convertStringToDate("2009-12-31"));
		assistDto.setRate(15);
		assistDto.setUpdown(1);
		assistDto.setCurrentPages(0);
		assistDto.setTotalPages(0);

		this.doNetGainSearch();
		
		return "search";
	}
	
	/**
	 * 处理”净资产收益率（每股收益/每股净资产）“，用户搜索
	 * @return
	 * @throws Exception
	 */
	public String searchNetGain() throws Exception{
		if (!this.doNetGainSearch()) {
			System.out.println("prepare====================");
			return "prepare";
		}
		return "search";
	}
	
	/**
	 * 处理”净资产收益率（每股收益/每股净资产）“，详细信息显示
	 * @return
	 * @throws Exception
	 */
	public String netGainDetail() throws Exception{
		this.stoFinanceprofitDTOList=stoFinanceProfitService.queryStoFinanceprofitByStockCode(stockDTO.getStockCode());
		stockDTO=stoStockService.findByStockCode(stockDTO.getStockCode());
		
		return "detail";
	}
	
	private boolean doNetGainSearch(){
		Date startDate=assistDto.getStartDate();
		Date endDate=assistDto.getEndDate();
		double rate=assistDto.getRate();
		int updown=assistDto.getUpdown();
		System.out.println("开始时间="+DateFormatUtils.convertDateTimeToString(startDate)+";结束时间="
				+DateFormatUtils.convertDateTimeToString(endDate)+";百分比="+rate+";上升还是下降="+updown+"当前页"+assistDto.getCurrentPages()+"总共assistDto.getCurrentPages()"+assistDto.getCurrentPages());
		/**
		 * 设置当前页
		 */
		if (0 == assistDto.getCurrentPages()) {
			assistDto.setCurrentPages(1);
		}
		/**
		 * 偏移值= (当前第几页-1)*每页个数
		 */
		int offset = (assistDto.getCurrentPages() - 1) * limit;
		/**
		 * 查询得到的总记录数
		 */
		int num=stoFinanceProfitService.countSearchWithNetGain(startDate, endDate, rate,updown);
		System.err.println("--查询得到的总记录数-----"+num+"-----");
		/**
		 * 总共多少页
		 */
		int totalPages = (num / limit);
		if (num % limit > 0) {
			totalPages++;
		}
		
		if (assistDto.getCurrentPages() >totalPages) {
			assistDto.setCurrentPages(1); 
			return false;
		}
		System.out.println(totalPages+"总页数");
		assistDto.setTotalPages(totalPages);
		
		HashMap<String,List> map=stoFinanceProfitService.querySearchWithNetGain(startDate, endDate, rate,updown,offset,limit); 
		this.stoFinanceprofitDTOList=map.get("stoFinanceprofitDTOList");
		this.stockDTOList=map.get("stockDTOList");
		this.oldYearStoFinanceprofitDTOList=map.get("oldYearStoFinanceprofitDTOList");
		
//		for (StoFinanceprofitDTO StoFinanceprofitDTO : oldYearStoFinanceprofitDTOList) {
//			System.out.println("-----------"+StoFinanceprofitDTO.getId()+"---"+StoFinanceprofitDTO.getBusinessFee()+"------querySearchWithNetGain----------");
//		}
		return true;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	public FinanceProfitAction(){
		if (assistDto==null) {
			assistDto=new AssistDto();
		}
		if (stockDTO==null) {
			stockDTO=new StoStockDTO();
		}
		if (stoFinanceprofitDTOList==null) {
			stoFinanceprofitDTOList=new ArrayList<StoFinanceprofitDTO>();
		}
		if (stockDTOList==null) {
			stockDTOList=new ArrayList<StoStockDTO>();
		}
	}
	
	@Override
	public TbStoFinanceprofit getModel() {
		if (null==tbStoFinanceprofit) {
			this.tbStoFinanceprofit=new TbStoFinanceprofit();
		}
		return this.tbStoFinanceprofit;
	}


	public TbStoFinanceprofit getTbStoFinanceprofit() {
		return tbStoFinanceprofit;
	}


	public void setTbStoFinanceprofit(TbStoFinanceprofit tbStoFinanceprofit) {
		this.tbStoFinanceprofit = tbStoFinanceprofit;
	}

	public List<StoStockDTO> getStockDTOList() {
		return stockDTOList;
	}

	public void setStockDTOList(List<StoStockDTO> stockDTOList) {
		this.stockDTOList = stockDTOList;
	}

	public AssistDto getAssistDto() {
		return assistDto;
	}

	public void setAssistDto(AssistDto assistDto) {
		this.assistDto = assistDto;
	}

	public StoStockDTO getStockDTO() {
		return stockDTO;
	}

	public void setStockDTO(StoStockDTO stockDTO) {
		this.stockDTO = stockDTO;
	}

	public StoStockService getStoStockService() {
		return stoStockService;
	}

	public void setStoStockService(StoStockService stoStockService) {
		this.stoStockService = stoStockService;
	}

	public StoFinanceProfitService getStoFinanceProfitService() {
		return stoFinanceProfitService;
	}

	public void setStoFinanceProfitService(
			StoFinanceProfitService stoFinanceProfitService) {
		this.stoFinanceProfitService = stoFinanceProfitService;
	}

	public List<StoFinanceprofitDTO> getStoFinanceprofitDTOList() {
		return stoFinanceprofitDTOList;
	}

	public void setStoFinanceprofitDTOList(
			List<StoFinanceprofitDTO> stoFinanceprofitDTOList) {
		this.stoFinanceprofitDTOList = stoFinanceprofitDTOList;
	}

	public List<StoFinanceprofitDTO> getOldYearStoFinanceprofitDTOList() {
		return oldYearStoFinanceprofitDTOList;
	}

	public void setOldYearStoFinanceprofitDTOList(
			List<StoFinanceprofitDTO> oldYearStoFinanceprofitDTOList) {
		this.oldYearStoFinanceprofitDTOList = oldYearStoFinanceprofitDTOList;
	}
}
